package neu.soft.facade;

import neu.soft.constant.FinancialVoucherType;
import neu.soft.entity.DO.ReceiptJournal;
import neu.soft.entity.FinancialVoucher;
import neu.soft.exception.AppException;
import neu.soft.exception.AppExceptionCodeMsg;
import neu.soft.service.FinancialVoucherService;
import neu.soft.service.ReceiptJournalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;

@Component
public class ReceiptSourceHandler {


    @Autowired
    private ReceiptJournalService receiptJournalService;

    @Autowired
    private FinancialVoucherService financialVoucherService;


    //记录客户交易明细，录入
    public void addJournalService(ReceiptJournal receiptJournal){
        receiptJournalService.addJournal(receiptJournal);
    }

    //财务核算，生成结账凭证
    public FinancialVoucher generateSourceDocument(String oid){
        ReceiptJournal receiptJournal = receiptJournalService.findById(oid);
        FinancialVoucher financialVoucher = financialVoucherService.findByOrderId(oid);
        if(financialVoucher==null){
            System.out.println("==");
            return null;
        }
        if ("0".equals(financialVoucher.getVoucherType())){
            throw new AppException(AppExceptionCodeMsg.REPEAT_FAIL);
        }
        BigDecimal cost = financialVoucher.getPrice();
        BigDecimal pay = receiptJournal.getTotalPay();
        if (cost == null||pay==null){
            throw new AppException(AppExceptionCodeMsg.REQUEST_PARAM_EMPTY);
        }
        if (cost.compareTo(pay) == 0){
            financialVoucher.setVoucherType(FinancialVoucherType.LEDGER_SOURCE_DOCUMENT.getCode());
        }else {
            throw new AppException(AppExceptionCodeMsg.YYDZ_JDWGXSB);
        }


        return financialVoucher;
    }


}
